/*
 * Hidden Equity LLC
 * Author: Maksym Fedyay, Kyiv, mcsimf@gmail.com
 * Date: 29.02.2012
 */

package com.hiddenequity.adgroup_criterion_service;

import java.util.ArrayList;

import com.hiddenequity.acts.Act_Super;
import com.hiddenequity.utils.Converter;
import com.hiddenequity.utils.SOAPUtils;
import com.hiddenequity.utils.UrlBuncher;

public class AdGroupCriterionService {

	public static final String URL_ADGROUP_CRITERION_SERVICE = UrlBuncher.URL_BASE + "AdGroupCriterionService";
	
	/*
	 * Add KeyWord
	 */
	public static String addKeywordOperation(String keywordId, String adGroupId, String keyword, 
			String matchType, String maxCpc, String destignationUrl,
			boolean criterionUse){

		String destignationUrlTag = "";
		if(criterionUse)
			if(destignationUrl != null){
				destignationUrlTag = "<destinationUrl>" + destignationUrl +"</destinationUrl>";
			}
			
		//Convert amount to a microamount
		String maxCpcTag = "";
		if(criterionUse)
			if(!maxCpc.equals("")){
				maxCpcTag = 
						"<bids xsi:type='ManualCPCAdGroupCriterionBids'>" +
						 	"<maxCpc>" +
						 		"<amount>" +
					 				"<microAmount>" + Converter.strToMicroamount(maxCpc) +"</microAmount>" +
					 			"</amount>" +
					 		"</maxCpc>" +
					 	"</bids>";
			} 

		String SOAPBody = 
				"<soap:Body>" +
					"<mutate xmlns=" + SOAPUtils.XMLNS +
						"xmlns:xsi="+ SOAPUtils.XMLNSXSI  +">" +
					"<operations>" +
						"<operator>ADD</operator>" +
						"<operand xsi:type='BiddableAdGroupCriterion'>" +
						 	"<adGroupId>" + adGroupId + "</adGroupId>" +
						 	"<criterionUse>BIDDABLE</criterionUse>" +
						 	"<criterion xsi:type='Keyword'>" +	
						 		"<text>" + keyword + "</text>" +
						 		"<matchType>" + matchType +"</matchType>" +
						 	"</criterion>" +
						 	destignationUrlTag +
						 	maxCpcTag + 
						"</operand>" +
					"</operations>" +
				"</mutate>" +
			"</soap:Body>";
		
			String envelope = SOAPUtils.buildEnvelope( SOAPBody);
			return envelope;		
	}//keywordAddOperation
	
	
	
	/*
	 * Add negative keyword
	 */
	public static String addNegativeKeywordOperation(String adGroupId, String keywordId, 
			String keyword, String matchType){

		String SOAPBody = 
				"<soap:Body>" +
					"<mutate xmlns=" + SOAPUtils.XMLNS +
						"xmlns:xsi="+ SOAPUtils.XMLNSXSI  +">" +
					"<operations>" +
						"<operator>ADD</operator>" +
						"<operand xsi:type='NegativeAdGroupCriterion'>" +
						 	"<adGroupId>" + adGroupId + "</adGroupId>" +
						 	"<criterionUse>NEGATIVE</criterionUse>" +
						 	"<criterion xsi:type='Keyword'>" +
						 		"<id>" + keywordId + "</id>" +	
						 		"<text>" + keyword + "</text>" +
						 		"<matchType>" + matchType +"</matchType>" +
						 	"</criterion>" +
						"</operand>" +
					"</operations>" +
				"</mutate>" +
			"</soap:Body>";
		
			String envelope = SOAPUtils.buildEnvelope( SOAPBody);
			return envelope;
	}
	
	
	
	/*
	 * Keyword remove operation
	 */
	public static String keywordRemoveOperation(String adGroupId, String keywordId, 
			String keyword, String matchType){

		String SOAPBody = 
				"<soap:Body>" +
					"<mutate xmlns=" + SOAPUtils.XMLNS +
						"xmlns:xsi="+ SOAPUtils.XMLNSXSI  +">" +
					"<operations>" +
						"<operator>REMOVE</operator>" +
						"<operand xsi:type='BiddableAdGroupCriterion'>" +
						 	"<adGroupId>" + adGroupId + "</adGroupId>" +
						 	"<criterion xsi:type='Keyword'>" +	
						 		"<id>" + keywordId +"</id>" +
						 		"<text>" + keyword + "</text>" +
						 		"<matchType>" + matchType +"</matchType>" +
						 	"</criterion>" +
						"</operand>" +
					"</operations>" +
				"</mutate>" +
			"</soap:Body>";
		
			String envelope = SOAPUtils.buildEnvelope( SOAPBody);
			return envelope;		
	}//keywordRemoveOperation
	
	
	
	/*
	 * Get keyword list
	 */
	public static String getKeywordList(String adGroupId, int status, ArrayList<String> dateRange, boolean criterionUse){
		String SOAPBody = 
				"<soap:Body>" +
					"<get xmlns=" + SOAPUtils.XMLNS +
						"xmlns:xsi="+ SOAPUtils.XMLNSXSI  +">" +
					"<serviceSelector>" +
						"<fields>Status</fields>" +
						"<fields>Clicks</fields>" +
						"<fields>Cost</fields>" +
						"<fields>Ctr</fields>" +
						"<fields>AverageCpc</fields>" +
						"<fields>Impressions</fields>" +
						"<fields>KeywordText</fields>";		
						
		if(status != 0){
			String statusName = "";
			switch (status) {
				case Act_Super.STATUS_ACTIVE:
					statusName = "ACTIVE";
					break;
				case Act_Super.STATUS_PAUSED:
					statusName = "PAUSED";
					break;
				case Act_Super.STATUS_DELETED:
					statusName = "DELETED";
					break;
			}
			
			SOAPBody = SOAPBody + 
				"<predicates>" +
					"<field>Status</field>" +
					"<operator>EQUALS</operator>" +
					"<values>" + statusName + "</values>" +
				"</predicates>";
		}	
		
		// predicate criterion use
		String criterion = criterionUse ? "BIDDABLE" : "NEGATIVE";
		
		SOAPBody = SOAPBody + 
				"<predicates>" +
					"<field>CriterionUse</field>" +
					"<operator>EQUALS</operator>" +
					"<values>" + criterion + "</values>" +
				"</predicates>";
		
		SOAPBody = SOAPBody + "<predicates>" +
							"<field>AdGroupId</field>" +
							"<operator>EQUALS</operator>" +
							"<values>" + adGroupId + "</values>" +
						"</predicates>";
		
		if(!dateRange.get(0).equals("")){
			SOAPBody = SOAPBody +	"<dateRange>" +
					"<min>" + dateRange.get(0) +"</min>" +
					"<max>" + dateRange.get(1) +"</max>" +
					"</dateRange>";
		}	
		
		SOAPBody = SOAPBody + 
						"<ordering>" +
							"<field>Clicks</field>" +
							"<sortOrder>DESCENDING</sortOrder>" +
						"</ordering>" +
					"</serviceSelector>" +
				"</get>" +
			"</soap:Body>";
			String envelope = SOAPUtils.buildEnvelope( SOAPBody);
			return envelope;	
	}//getKeywordList
	
	
	
	/*
	 * Set keyword status
	 */
	static public String setKeywordStatus(String adGroupId, String keywordId, int status, String keyword, String matchType){
		String statusName = "";
		
		if(status != 0){
			switch (status) {
				case Act_Super.STATUS_ACTIVE:
					statusName = "ACTIVE";
					break;
				case Act_Super.STATUS_PAUSED:
					statusName = "PAUSED";
					break;
				case Act_Super.STATUS_DELETED:
					statusName = "DELETED";
					return keywordRemoveOperation(adGroupId, keywordId, keyword, matchType);
			}
		}
		
		String SOAPBody = 
			"<soap:Body>" +
				"<mutate xmlns=" + SOAPUtils.XMLNS +
					"xmlns:xsi="+ SOAPUtils.XMLNSXSI  +">" +
					"<operations>" +
						"<operator>SET</operator>" +
						"<operand xsi:type='BiddableAdGroupCriterion'>" +
							"<adGroupId>" + adGroupId + "</adGroupId>" +
							"<criterion xsi:type='Keyword'>" +
					 			"<id>" + keywordId + "</id>" +
						 		"<text>" + keyword + "</text>" +
						 		"<matchType>" + matchType +"</matchType>" +
					 		"</criterion>" +
					 		"<userStatus>"+ statusName + "</userStatus>" +
						"</operand>" +
					"</operations>" +
				"</mutate>" +
			"</soap:Body>";		
		
		String envelope = SOAPUtils.buildEnvelope(SOAPBody);
		return envelope;		
	}//setKeywordStatus
	
	
	
	/*
	 * Get keyword by Id
	 */
	public static String getKeywordById(String adGroupId, String keywordId) {
		String SOAPBody = 
				"<soap:Body>" +
					"<get xmlns=" + SOAPUtils.XMLNS +
						"xmlns:xsi="+ SOAPUtils.XMLNSXSI  +">" +
						"<serviceSelector>" +
							"<fields>KeywordText</fields>" +
							"<fields>KeywordMatchType</fields>" +
							"<fields>DestinationUrl</fields>" +
							"<fields>MaxCpc</fields>" +
							"<predicates>" +
								"<field>AdGroupId</field>" +
								"<operator>EQUALS</operator>" +
								"<values>" + adGroupId + "</values>" +
							"</predicates>"+ 
							"<predicates>" +
								"<field>Id</field>" +
								"<operator>EQUALS</operator>" +
								"<values>" + keywordId + "</values>" +
						"</predicates>"+ 
						"</serviceSelector>" +
					"</get>" +
				"</soap:Body>";
			String envelope = SOAPUtils.buildEnvelope( SOAPBody);
			return envelope;	
	}//getKeywordById
	
}
